/** 引入类型 */
import type {Route} from './index.type'

/** 引入路由相关的资源 */
import router, {modules} from './index'
/** 引入vuex实例 */
import store from '@/store'

/** 动态路由实现基础组件 */
/** 引入全局Layout组件 */
import Layout from '@/layout/index.vue'
/** 引入多级菜单控制器组件 */
import MenuBox from '@/components/menu/index.vue'
/** 引入带有系统自定义name的组件，方便keep-alive实现 */

/** 引入需要权限的Modules */
import Dashboard from './modules/dashboard'
import Document from './modules/document'
import Pages from './modules/pages'
import Menu from './modules/menu'
import Component from './modules/component'
import Directive from './modules/directive'
import SystemManage from './modules/systemManage'
import Chart from './modules/chart'
import Print from './modules/print'
import Community from './modules/community'
import {createNameComponent} from './createNode'

let asyncRoutes: Route[] = [
	...Dashboard,
	...Document,
	...Component,
	...Pages,
	...Menu,
	...Directive,
	...Chart,
	// ...SystemManage,
	...Print,
	...Community,
]

/**
 * @name 动态路由的权限新增，供登录后调用
 * @other 如果需要进行后端接口控制菜单的话，请在此拿到后端的菜单树与asyncRoutes对比，生成一个新的值
 */

async function addRoutes(data: Route[]) {
	// 已验证完成，下面代码添加的可以实时同步至菜单中去，可以添加setTimeout(() => {}) 模拟异步代码的操作
	// 利用前端路由表模拟后端数据问题
	return new Promise<void>((resolve) => {
		asyncRoutes = [...data, ...asyncRoutes]
		asyncRoutes.forEach(item => {
			modules.push(item)
			router.addRoute(item)
		})
		resolve()
	})
}

// 重构路由并支持外链
function eachData(data: Route[], type: number, redirect: string) {
	data.forEach(menu => {
		// 设置 meta
		menu.meta.type = menu.menuType
		menu.meta.cache = menu.cache

		//判断菜单类型
		if (menu.meta.type == 2) {
			menu.meta.url = "//" + menu.component
			menu.component = Layout
			// 判断是否顶级菜单
			if (type === 0) {
				menu.path = '/' + menu.path
				menu.component = Layout
				menu.alwayShow = true
			} else {

			}
		} else {
			let component = loadView(menu.component)
			menu.component = component;
			// 判断是否顶级菜单
			if (type === 0) {
				menu.path = '/' + menu.path
				menu.component = Layout
				menu.alwayShow = true
			} else {

			}
		}

		if (menu.children && menu.children.length > 0) {
			if (type === 0) {
				menu.redirect = menu.path + "/" + menu.children[0].path
			} else {
				menu.redirect = redirect + "/" + menu.children[0].path
			}
			eachData(menu.children, type + 1, redirect)
		}
	})
}

function menuEach(data: Route[]) {
	let menus: Route[] = []
	data.forEach(item => {
		let menu: Route = {
			path: item.path,
			component: item.component,
			meta: item.meta
		}
		if (item.children && item.children.length > 0) {
			menu.children = menuEach(item.children)
		}
		menus.push(menu)
	});
	return menus;
}

const loadView: any = (view: any) => {
	const comp = import.meta.glob("../views/**/**.vue")
	return comp[`../views/${view}.vue`]
}

/**
 * @des 登录了之后会执行这个方法，实现动态路由的功能
 */
export async function getAuthRoutes() {
	// 判断token是否存在，存在则调用添加路由的方法
	if (store.state.user.token) {
		store.dispatch('user/getInfo').then(res => {
			if (res.code !== 200) {
				store.dispatch('user/loginOut').then(() => {
					// location.reload()
				})
			}
		})
		if (store.state.user.menu) {
			let menu: Route[] = JSON.parse(JSON.stringify(store.state.user.menu));
			eachData(menu, 0, "");
			menu = menuEach(menu);
			await addRoutes(menu)
		}
	}
}

